

package com.kimo.shop.delivery.admin.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kimo.shop.bean.event.GetTransportNamesEvent;
import com.kimo.shop.bean.param.TransportParam;
import com.kimo.shop.delivery.comment.model.Transport;
import com.kimo.shop.delivery.comment.service.TransportService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 获取所有的运费模板名称
 * @author kimo-taozhouchuan
 */
@Component("getTransportNamesListener")
@AllArgsConstructor
public class GetTransportNamesListener {

    private TransportService transportService;
    private MapperFacade mapperFacade;

    /**
     * 获取所有的运费模板名称
     */
    @EventListener(GetTransportNamesEvent.class)
    public void getNamesEvnet(GetTransportNamesEvent event) {
        List<Transport> transports = transportService.list(new LambdaQueryWrapper<Transport>().eq(Transport::getShopId, event.getShopId()));
        if(CollectionUtils.isNotEmpty(transports)) {
            List<TransportParam> transportParams = event.getTransportParams();
            transportParams.addAll(mapperFacade.mapAsList(transports, TransportParam.class));
        }
    }

}
